<?php

App::uses('AppController', 'Controller');

/**
 * Payments Controller
 *
 * @property Payment $Payment
 */
class PaymentsController extends AppController {

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('*');
    }

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        //TODO  
        $this->Payment->recursive = 0;
        $this->set('payments', $this->paginate());
        $this->redirect('/');
    }

    /**
     * view method
     *
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        //TODO
        $this->redirect('/');
        /*
          $this->Payment->id = $id;
          if (!$this->Payment->exists()) {
          throw new NotFoundException(__('Invalid payment'));
          }
          $this->set('payment', $this->Payment->read(null, $id)); */
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        $this->redirect('/');
        /*
          if ($this->request->is('post')) {
          $this->Payment->create();
          if ($this->Payment->save($this->request->data)) {
          $this->Session->setFlash(__('The payment has been saved'));
          $this->redirect(array('action' => 'index'));
          } else {
          $this->Session->setFlash(__('The payment could not be saved. Please, try again.'));
          }
          }
          $subscriptions = $this->Payment->Subscription->find('list');
          $paymentTypes = $this->Payment->PaymentType->find('list');
          $this->set(compact('subscriptions', 'paymentTypes')); */
    }

    /**
     * edit method
     *
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        //TODO
        $this->redirect('/');
        $this->Payment->id = $id;
        if (!$this->Payment->exists()) {
            throw new NotFoundException(__('Invalid payment'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Payment->save($this->request->data)) {
                $this->Session->setFlash(__('The payment has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The payment could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->Payment->read(null, $id);
        }
        $subscriptions = $this->Payment->Subscription->find('list');
        $paymentTypes = $this->Payment->PaymentType->find('list');
        $this->set(compact('subscriptions', 'paymentTypes'));
    }

    /**
     * delete method
     *
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        //TODO
        $this->redirect('/');
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Payment->id = $id;
        if (!$this->Payment->exists()) {
            throw new NotFoundException(__('Invalid payment'));
        }
        if ($this->Payment->delete()) {
            $this->Session->setFlash(__('Payment deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Payment was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

    public function admin_index() {
        $this->Payment->recursive = 1;
        /* $this->paginate['Payment']  = array(
          'contain' => 'Subscription'
          ); */

        $conditions = array(
        );
        $this->set('payments', $this->paginate('Subscription'));
    }

    public function admin_view($subId) {
        $this->set('payment', $this->Payment->Subscription->read(null, $subId));
    }

    public function admin_updateStatus($id, $status) {
        $this->Payment->id = $id;
        if ($this->Payment->exists()) {

            if ($status == 1) {
                $this->Payment->saveField('payment_status', PAYMENT_STATUS_COMPLETED);
            } else {
                $this->Payment->saveField('payment_status', PAYMENT_STATUS_PENDING);
            }
            $this->Session->setFlash(__('Payment has been updated.'));
        } else {
            $this->Session->setFlash(__('Invalid payment'));
        }
        $this->redirect(array('action' => 'index'));
    }
    public function admin_print($subscriberId) {
        $this->layout = 'invoice';
        $this->Payment->Subscription->id = $subscriberId;
        
                    $this->loadModel('Setting');
            $company = $this->Setting->getCompany();
            
        if ($this->Payment->Subscription->exists()) {
            $this->set('payment', $this->Payment->Subscription->read(null, $subscriberId));
            $this->set('company', $company);
	} else {
            $this->redirect(array('action' => 'index'));
        }
    }

}
